function OrderComponent(template) {
	
	this.model = null;  // 维护model
	this.template = template;
	

	this.getDatawithState=function(mid,state){

		up.ajax("PATCH","/meat/sec/order/get/"+mid+"/state/"+state,null,null,this,
			function(callback){
				console.log(callback)
				if(callback.code == 200){
					model = null;
					model = callback.data;
					_render(model,state);
				}else{
					swal("DB is wrong!");
				}
				
			});
	}

	function _changeState(oid,state){
		
		up.ajax("PATCH","/meat/api/order/"+oid+"/affirm/"+state,null,null,this,
			function(callback){
				console.log(callback)
				if(callback.code == 200){
					model=model.filter(function(i){
						return i.id!=callback.data;
					});
					_render(model,state);
				}else{
					swal("DB is wrong!");
				}
				
			});

	}



	function _render(data,state){
		template.empty();
		 if(state == 0){
			 $("#orderTemplate").find("#receive_btn").css("display","block").end()
			 .find("#refuse_btn").css("display","block").end()
			 .find("#delivery_btn").css("display","none").end()
		 }else if(state==1){
			 $("#orderTemplate").find("#receive_btn").css("display","none").end()
			 .find("#refuse_btn").css("display","none").end()
			 .find("#delivery_btn").css("display","block").end()
		 }else if(state==4 || state==3){
			 $("#orderTemplate").find("#receive_btn").css("display","none").end()
			 .find("#refuse_btn").css("display","none").end()
			 .find("#delivery_btn").css("display","none").end()
		 }
		data.forEach((item)=>{

			$("#orderTemplate").clone(true)
			.css("display","block")
			.find(".panel-heading").attr("href","#"+item.id).end()
			.find(".panel-collapse").attr("id",item.id).end()
			.find(".cname").text(item.username).end()
			.find(".id").text(item.id).end()
			.find(".currenttime").text(tool.formatDateTime(item.create_time)).end()
			.find(".total").text("￥ "+item.total).end()
			.find(".panel-body").html(_renderFoods(item.orderItem)).end()
			.appendTo(template);
			
			
			$("button[id=receive_btn]").on("click",function(){
				swal({
					title: "Are you sure?",
					text: "You will recevice the order and pls send on time!",
					type: "warning",
					showCancelButton: true,
					confirmButtonColor: "#DD6B55",
					confirmButtonText: "Yes, receive it!",
					closeOnConfirm: true
				},
				function(){
					_changeState(item.id,1)
					
				});
			});

			$("button[id=refuse_btn]").on("click",function(){
				swal({
					title: "Are you sure?",
					text: "You will refuse the order!",
					type: "warning",
					showCancelButton: true,
					confirmButtonColor: "#DD6B55",
					confirmButtonText: "Yes, refuse it!",
					closeOnConfirm: true
				},
				function(){
					_changeState(item.id,2)
				});
			});
			
			$("button[id=delivery_btn]").on("click",function(){
				swal({
					title: "Are you sure?",
					text: "You will delivery the order!",
					type: "warning",
					showCancelButton: true,
					confirmButtonColor: "#DD6B55",
					confirmButtonText: "Yes, delivery it!",
					closeOnConfirm: true
				},
				function(){
					_changeState(item.id,3)
				});
			});
			
		});
		

	}



	function _renderFoods(items){

		this.foodtable = '<div class="table-responsive">'
		+'<table class="table table-striped">'
		+'<thead>'
		+'<tr>'
		
		+'<th>Name</th>'
		+'<th>Img</th>'
		+'<th>Price</th>'
		+'<th>Count</th>'
		+'<th>Total</th>'

		+'</tr>'
		+'</thead>'
		+'<tbody>'
		+ _renderFood(items)
		+'</tbody>'
		+'</table>'
		+'</div>'

		return foodtable;
	}

	function _renderFood(items){

		var foodtbody = '';

		items.forEach((item)=>{

			foodtbody +=_renderAFood(item);
		})
		return foodtbody;
	}

	function _renderAFood(item){

		var res = '<tr>'
		+'<th>'+item.food+'</th>'
		+'<th><img style="width:40px;height:40px" src='+item.image+' name='+item.id+' class="img"/></td>'
		+'<th>'+item.price+'</th>'
		+'<th>'+item.count+'</th>'
		+'<th>'+item.price*item.count+'</th><tr>';
	// +'<th><Button name='+item.id+' class="modify">Receive</Button></th>'
	// +'<th><Button name='+item.id+' class="delete">Refuse</Button></th></tr>';


	$(".img[name="+item.id+"]").on("click",function(){
		var src = $(this).attr("src");
		$("#modal-img-target").attr("src", src);
		$("#modal-img").modal("show");
	})

	return res;
}
}



